<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Object.assign</title>
</head>

<body>
  <script>
    // Object.assign不存在的属性会复制，存在的属性后者会覆盖前者


    // 编写不覆盖已经存在的说属性
    function merge(target, ...sources) {
      // console.log(target);
      console.log(sources);
      for (source of sources) {
        console.log(source);
        for (let key of Object.keys(source)) {
          console.log(key);
          if (!(key in target)) {
            target[key] = source[key]
          }
        }
      }
      return target
    }

    console.log(merge({ x: 1 }, { x: 2, y: 2 }, { y: 3, z: 4 }));
  </script>
</body>

</html>